import Fetch from "../../../utils/fetch";
import API from "../../../api/quality";
import Vue from "vue";
import LibsCommonBasics from "libs-common-basics";

const state = {
    // 到货通知高级搜索初始状态
    queryAdvanceSearchVisible: false,
    // 到货通知增加通知modal
    modalVisible: false,
    // 列表数据
    dataSource: [],
    // 列表项
    columns: [],
    // 列表数据加载状态
    loading: false,
    // 所选中的行
    selectedRowKeys: [],
    // 高级搜索数据
    formData: {},
    // 分页
    pager: {
        pageNum: LibsCommonBasics.LibsPagination.props.pageNum.default,
        pageSize: LibsCommonBasics.LibsPagination.props.pageSize.default,
        total: LibsCommonBasics.LibsPagination.props.total.default,
        current: LibsCommonBasics.LibsPagination.props.current.default,
    },
};

const mutations = {
    // 高级搜索状态改变
    queryModule(state, params) {
        state.queryAdvanceSearchVisible = params;
    },
    // 到货通知modal状态
    modalModule(state, params) {
        state.modalVisible = params;
    },
    // 获取dataSource
    getDataSource(state, res) {
        state.dataSource = res.list;
        state.columns = res.fields;
    },
};

const actions = {
    // 新增
    addRequest({
        commit
    }, params) {
        let url = API.API_TASK_ADD;
        state.loading = true;
        return new Promise(resolve => {
            Fetch(url, {
                method: "POST",
                body: params,
            }).then(res => {
                state.loading = false;
                resolve(res);
            });
        });
    },
    // 编辑
    editRequest({
        commit
    }, params) {
        let url = API.API_TASK_EDIT;
        state.loading = true;
        return new Promise(resolve => {
            Fetch(url, {
                method: "POST",
                body: params,
            }).then(res => {
                state.loading = false;
                resolve(res);
            });
        });
    },
    // 查询
    queryRequest({
        commit
    }, params) {
        let url = API.API_TASK_QUERY;
        state.loading = true;
        console.log('params', params)
        return new Promise(resolve => {
            Fetch(url, {
                method: "POST",
                body: params,
            }).then(res => {
                if (parseInt(res.code) == 1) {
                    state.pager.pageNum = parseInt(res.data.current);
                    state.pager.pageSize = parseInt(res.data.size);
                    state.pager.total = parseInt(res.data.total);
                    state.pager.current = parseInt(res.data.current);
                    commit("getDataSource", res.data);
                } else {
                    res.msg && Vue.prototype.$message.error(res.msg);
                }
                state.loading = false;
                resolve(res);
            });
        });
    },
    // 查询单个
    viewRequest({
        commit
    }, params) {
        let url = API.API_TASK_VIEW;
        return new Promise(resolve => {
            Fetch(url, {
                method: "post",
                body: params,
            }).then(res => {
                resolve(res);
                console.log(res);
            });
        });
    },

    // 删除
    deleteRequest({
        commit
    }, params) {
        let url = API.API_TASK_DELETE;
        return new Promise(resolve => {
            Fetch(url, {
                method: "post",
                body: params,
            }).then(res => {
                resolve(res);
                console.log(res);
            });
        });
    },

    // 确认
    confirmRequest({
        commit
    }, params) {
        let url = API.API_TASK_CONFIRM;
        return new Promise(resolve => {
            Fetch(url, {
                method: "POST",
                body: params,
            }).then(res => {
                resolve(res);
                console.log(res);
            });
        });
    },
    // 提交
    submitRequest({
        commit
    }, params) {
        let url = API.API_TASK_SUBMIT;
        return new Promise(resolve => {
            Fetch(url, {
                method: "POST",
                body: params,
            }).then(res => {
                resolve(res);
                console.log(res);
            });
        });
    },
    // 中止
    stopRequest({
        commit
    }, params) {
        let url = API.API_TASK_STOP;
        return new Promise(resolve => {
            Fetch(url, {
                method: "POST",
                body: params,
            }).then(res => {
                resolve(res);
                console.log(res);
            });
        });
    },
    // 复检
    reviewRequest({
        commit
    }, params) {
        let url = API.API_TASK_REVIEW;
        return new Promise(resolve => {
            Fetch(url, {
                method: "POST",
                body: params,
            }).then(res => {
                resolve(res);
                console.log(res);
            });
        });
    },
    // 查询标准
    queryStandardRequest({
        commit
    }, params) {
        let url = API.API_TASK_CONFIRM;
        return new Promise(resolve => {
            Fetch(url, {
                method: "POST",
                body: params,
            }).then(res => {
                resolve(res);
                console.log(res);
            });
        });
    },
    // 详情启动
    startDetailRequest({
        commit
    }, params) {
        let url = API.API_TASK_DETAIL_STARTUP;
        return new Promise(resolve => {
            Fetch(url, {
                method: "POST",
                body: params,
            }).then(res => {
                resolve(res);
                console.log(res);
            });
        });
    },
    // 详情录入
    entryDetailRequest({
        commit
    }, params) {
        let url = API.API_TASK_DETAIL_ENTRY;
        return new Promise(resolve => {
            Fetch(url, {
                method: "POST",
                body: params,
            }).then(res => {
                resolve(res);
                console.log(res);
            });
        });
    },
    // 详情提交
    submitDetailRequest({
        commit
    }, params) {
        let url = API.API_TASK_DETAIL_SUBMIT;
        return new Promise(resolve => {
            Fetch(url, {
                method: "POST",
                body: params,
            }).then(res => {
                resolve(res);
                console.log(res);
            });
        });
    },
};

const getters = {};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions,
};